package com.jinzhi.jzweb.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.type.EnumErrorCode;
import com.jinzhi.common.utils.EhCacheUtil;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.dao.ArchivesDao;
import com.jinzhi.jzweb.domain.ApplicationRecordDO;
import com.jinzhi.jzweb.domain.ArchivesDO;
import com.jinzhi.jzweb.domain.ExaminationCostDO;
import com.jinzhi.jzweb.domain.TrainingCertificatesDO;
import com.jinzhi.jzweb.pojo.ArchivesVo;
import com.jinzhi.jzweb.pojo.ErrorCodeVo;
import com.jinzhi.jzweb.service.ArchivesService;
import com.jinzhi.jzweb.service.ExaminationCostService;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import com.jinzhi.sys.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2021-07-21 16:50:07 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/statistics")
public class StatisticsController extends BaseController {
	@Autowired
	private ArchivesService archivesService;
	@Autowired
	private ArchivesDao archivesDao;
	@Autowired
	private UserService userService;

	@Autowired
	private ExaminationCostService examinationCostService;

	@Autowired
	private
	@GetMapping()
	@RequiresPermissions("jzweb:statistics:statistics")
	String Archives(){
		//return "jzweb/archives/archives";

		return "jzweb/statistics/statistics";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:archives:archives")
	public Result<Page<ArchivesDO>> list(ArchivesDO archivesDTO){
//		// 查询列表数据
//		Page<ArchivesDO> page = archivesService.selectPage(getPage(ArchivesDO.class),
//					archivesService.convertToEntityWrapper().orderBy("id", false));
		Page<ArchivesDO> page =new Page<>();
		page.setTotal(32);
		page.setCurrent(0);
		page.setSize(32);
	Wrapper<ArchivesDO> wrapper = new EntityWrapper<ArchivesDO>().groupBy("province");
	List<ArchivesDO> list=	archivesService.selectList(wrapper);
		List<ArchivesDO> collect1 =   (List<ArchivesDO>) EhCacheUtil.get( ":archives");
		if(collect1==null){
			for(ArchivesDO li:list){

				Wrapper<ArchivesDO> wrapper1 = new EntityWrapper<ArchivesDO>();
				wrapper1.eq("province",li.getProvince());
				List<ArchivesDO> list1=	archivesService.selectList(wrapper1);
				Integer sum=0;
				for(ArchivesDO li1 :list1){
					Integer sum1=	archivesDao.statistice(li1.getSchoolName());
					if(sum1!=null){
						sum=sum+sum1;
					}

				}
				li.setSum(sum);
			}
		}else {
			 	page.setRecords(collect1);
			return Result.ok(page);

		}

		List<ArchivesDO> collect = list.stream().sorted(Comparator.comparing(ArchivesDO::getSum,Comparator.reverseOrder())).collect(Collectors.toList());

//		List list = new ArrayList();
//		for (int i = 0; i < page.getRecords().size(); i++) {
//				ArchivesDO obj = (ArchivesDO) page.getRecords().get(i);
//		//UserDO userDO= userService.getByUserId(Long.valueOf(obj.getPerson()));
//		//	obj.setPersonNmae(userDO.getName());
//			list.add(obj);
//		}.


		try {
			EhCacheUtil.setValue("archives" , ":archives",collect);
		} catch (Exception e) {
			e.printStackTrace();
		}

		page.setRecords(collect);
        return Result.ok(page);
	}

}
